#!/bin/bash
helpinfo() {
        echo "Usage:"
        echo "$0 [-u User Name in aliyun][-p Password][-n Image Name][-t Image Tag][-r Remote Image Name][-d Only download dependencies]"
        exit -1
}

download(){
    mvn -f pom.xml dependency:copy-dependencies
    exit 0
}

TAG=`date +%Z%y%m%d`
while getopts ':u:p:n:t:r:hd' OPT; do
    case $OPT in
        u) USERNAME="$OPTARG";;
        n) IMAGENAME="$OPTARG";;
        p) PASSWD="$OPTARG";;
        t) TAG="$OPTARG";;
        r) REMOTE="$OPTARG";;
        d) download;;
        h) helpinfo;;
    esac
done
REGISTRY_URL=registry.cn-chengdu.aliyuncs.com
docker stop $IMAGENAME
docker rm $IMAGENAME
docker build -t $USERNAME/$IMAGENAME .
docker login --username $USERNAME --password $PASSWD $REGISTRY_URL
docker tag $USERNAME/$IMAGENAME $REGISTRY_URL/$REMOTE/$IMAGENAME:$TAG
docker push $REGISTRY_URL/$REMOTE/$IMAGENAME:$TAG
echo "*--------------------PUSH INFO--------------------*"
echo "* REGISTRY: $REGISTRY_URL"
echo "* REMOTE  : $REMOTE"
echo "* IMAGE   : $IMAGENAME"
echo "* TAG     : $TAG"
echo "*-------------------------------------------------*"
docker rmi $USERNAME/$IMAGENAME
docker rmi $(docker images -a | grep none | awk '{print $3}')
